package com.siashan.toolkit.lock4j;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

/**
 * 分布式锁自动配置器
 *
 * @author siashan
 * @since v1.0.1
 */
@Configuration
@EnableConfigurationProperties(Lock4jProperties.class)
public class Lock4jAutoConfiguration {



    @Bean
    @ConditionalOnMissingBean
    public LockTemplate lockTemplate() {
        LockTemplate lockTemplate = new LockTemplate();
        lockTemplate.setProperties(lock4jProperties());
        return lockTemplate;
    }

    /**
     * lock4j配置文件
     * @return lock4j配置文件
     */
    @Bean
    @ConditionalOnMissingBean
    public Lock4jProperties lock4jProperties() {
        return new Lock4jProperties();
    }


    @Bean
    @ConditionalOnMissingBean
    public LockInterceptor lockInterceptor(LockTemplate lockTemplate) {
        return new LockInterceptor(lockTemplate);
    }

    @Bean
    @ConditionalOnMissingBean
    public LockAnnotationAdvisor lockAnnotationAdvisor(LockInterceptor lockInterceptor) {
        return new LockAnnotationAdvisor(lockInterceptor, Ordered.HIGHEST_PRECEDENCE);
    }

    @Bean
    @ConditionalOnMissingBean
    public NoRepeatInterceptor noRepeatInterceptor(LockTemplate lockTemplate) {
        return new NoRepeatInterceptor(lockTemplate);
    }



    @Bean
    @ConditionalOnMissingBean
    public NoRepeatAnnotationAdvisor noRepeatAnnotationAdvisor(NoRepeatInterceptor noRepeatInterceptor) {
        return new NoRepeatAnnotationAdvisor(noRepeatInterceptor, Ordered.HIGHEST_PRECEDENCE);
    }

}
